package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import org.ilovecode.Utilities;
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Gallery extends Screen {
		
		//{ region Stage elements
		
		public var imageContainer:MovieClip;
		public var thumbContainer:MovieClip;
		public var thumbContainerMask:MovieClip;
		public var scrollUpButton:SimpleButton;
		public var scrollDownButton:SimpleButton;
		public var imageNameTextField:TextField;
		public var progressBar:MovieClip;
		
		//} endregion
		
		private var galleryItems:Array;
		private var numItemsLoaded:int = 0;
		private var activeImage:Sprite = null;
		private var scrollDirection:String = "";
		private var scrollSpeed:int = 5;
		
		public function Gallery() {
			super(ScreenName.GALLERY, ScreenGroup.APARTMENTS, "galleryApartments");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			progressBar.gotoAndPlay(0);
			galleryItems = new Array();
			Utilities.loadURL("Content/Images/Galerija/gallery.xml", loadGalleryCompleted);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			scrollUpButton.addEventListener(MouseEvent.MOUSE_OVER, scrollUpButtonMouseOver);
			scrollUpButton.addEventListener(MouseEvent.MOUSE_OUT, scrollUpButtonMouseOut);
			scrollDownButton.addEventListener(MouseEvent.MOUSE_OVER, scrollDownButtonMouseOver);
			scrollDownButton.addEventListener(MouseEvent.MOUSE_OUT, scrollDownButtonMouseOut);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			scrollUpButton.removeEventListener(MouseEvent.MOUSE_OVER, scrollUpButtonMouseOver);
			scrollUpButton.removeEventListener(MouseEvent.MOUSE_OUT, scrollUpButtonMouseOut);
			scrollDownButton.removeEventListener(MouseEvent.MOUSE_OVER, scrollDownButtonMouseOver);
			scrollDownButton.removeEventListener(MouseEvent.MOUSE_OUT, scrollDownButtonMouseOut);
		}
		
		
		//{ region Handlers
		
		private function scrollUpButtonMouseOver(event:MouseEvent):void {
			scrollDirection = "up";
			this.addEventListener(Event.ENTER_FRAME, scroll);
		}
		
		private function scrollUpButtonMouseOut(event:MouseEvent):void {
			this.removeEventListener(Event.ENTER_FRAME, scroll);
			scrollDirection = "";
		}
		
		private function scrollDownButtonMouseOver(event:MouseEvent):void {
			scrollDirection = "down";
			this.addEventListener(Event.ENTER_FRAME, scroll);
		}
		
		private function scrollDownButtonMouseOut(event:MouseEvent):void {
			this.removeEventListener(Event.ENTER_FRAME, scroll);
			scrollDirection = "";
		}
		
		private function scroll(event:Event):void {
			if (scrollDirection == "up") {
				if (thumbContainer.y >= 0) {
					thumbContainer.y = 0;
				} else {
					thumbContainer.y += scrollSpeed;
				}
			} else if (scrollDirection == "down") {
				if (thumbContainer.y <= -(thumbContainer.height - thumbContainerMask.height)) {
					thumbContainer.y = -(thumbContainer.height - thumbContainerMask.height);
				} else {
					thumbContainer.y -= scrollSpeed;
				}
			} else {
				throw new Error("Illegal scroll direction");
			}
		}
		
		private function loadGalleryCompleted(event:Event):void {
			var xml:XML = XML(event.target.data);
			
			if (xml.children().length() > 0) {
				for each(var item:XML in xml.children()) {
					var gi:GalleryItem = new GalleryItem(item.name, item.image, item.thumb);
					gi.addEventListener(Event.COMPLETE, galleryItemReady);
					galleryItems.push(gi);
				}
			} else {
				throw new Error("Gallery images not found");
			}
		}
		
		private function galleryItemReady(event:Event):void {
			numItemsLoaded++;
			if (numItemsLoaded == galleryItems.length) {
				configThumbs();
				progressBar.visible = false;
			}
		}
		
		private function thumbClick(event:MouseEvent):void {
			var gi:GalleryItem = getGalleryItemByThumbName(event.currentTarget.name);
			showItem(gi);
		}
		
		//} endregion
		
		//{ region Screen
		
		private function configThumbs():void {
			var isFirst:Boolean = true;
			for each (var item:GalleryItem in galleryItems) {
				var thumb:Sprite = item.thumb;
				thumb.name = item.name + "_thumb";
				thumb.x = 0;
				thumb.y = thumbContainer.height;
				thumb.addEventListener(MouseEvent.CLICK, thumbClick);
				thumbContainer.addChild(thumb);
				
				//Show first item
				if (isFirst) {
					showItem(getGalleryItemByThumbName(thumb.name));
					isFirst = false;
				}
				
			}
		}
		
		private function showItem(gi:GalleryItem):void {
			if (gi != null) {
				var image:Sprite = gi.image;
				image.name = gi.name;
				image.x = (imageContainer.width - image.width) / 2;
				image.y = (imageContainer.height - image.height) / 2;
				
				if (activeImage != null) {
					imageNameTextField.text = "";
					imageContainer.removeChild(activeImage);
				}
				//imageNameTextField.text = gi.name;
				imageContainer.addChild(image);
				activeImage = image;
			} else {
				throw new Error("Gallery item not found");
			}
		}
		
		//} endregion
		
		//{ region Helpers
		
		private function getGalleryItemByThumbName(name:String):GalleryItem {
			for each (var item:GalleryItem in galleryItems) {
				if ((item.name + "_thumb") == name) {
					return item;
				}
			}
			return null;
		}
		
		//} endregion
		
	}
	
}